package com.purplesoft.zettl.Adapters;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;

import android.content.Context;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.purplesoft.zettl.R;
import com.purplesoft.zettl.DatabaseViews.VwPurchaseItems;

public class ItemListAdapter extends CursorAdapter {

	// TextView identifier for references from item_list_row.xml
	private TextView tvItem, tvAmountMeasure, tvItemDescriptionManufacturer,
			tvCategory, tvCategoryColor, tvPriceMeasure;
	
	private ImageView ivItemImg;

	// layout inflater object variable
	private LayoutInflater inflater;

	// private integer variables for column indices from
	// "VwPurchaseItems"
	private int item, itemImage, amount, itemDescription, manufacturer,
			measureShort, categoryName, categoryColor, price;

	private DecimalFormat decimalFormat;
	private DecimalFormatSymbols decimalFormatSymbol;
	private char decimalSeparator;
	
	// public constructor for ListItemAdapter
	public ItemListAdapter(Context context, Cursor cursor) {
		super(context, cursor);
		// Instantiate layout inflater object
		inflater = LayoutInflater.from(context);
		// get column indices for columns "Item", "ItemImg", "Amount"
		// "ItemDescription", "Manufacturer", "Measure", "CategoryName",
		// "CategoryColor", "Price"
		// from "VwPurchaseItems"
		item = cursor.getColumnIndex(VwPurchaseItems.Columns.Item);
		itemImage = cursor.getColumnIndex(VwPurchaseItems.Columns.ItemImg);
		amount = cursor.getColumnIndex(VwPurchaseItems.Columns.Amount);
		itemDescription = cursor
				.getColumnIndex(VwPurchaseItems.Columns.ItemDescription);
		manufacturer = cursor.getColumnIndex(VwPurchaseItems.Columns.Manufacturer);
		measureShort = cursor.getColumnIndex(VwPurchaseItems.Columns.MeasureShort);
		categoryName = cursor.getColumnIndex(VwPurchaseItems.Columns.CategoryName);
		categoryColor = cursor.getColumnIndex(VwPurchaseItems.Columns.CategoryColor);
		price = cursor.getColumnIndex(VwPurchaseItems.Columns.Price);

		// Dezimaltrennzeichen ermitteln
		decimalFormat = (DecimalFormat) DecimalFormat.getInstance();
		decimalFormatSymbol = decimalFormat.getDecimalFormatSymbols();
		decimalSeparator = decimalFormatSymbol.getDecimalSeparator();
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		// Make references for tvItem, tvAmountMeasure
		// tvItemDescriptionManufacturer, tvCategory
		// tvCategoryColor, tvPriceMeasure from item_list_row.xml
		tvItem = (TextView) view.findViewById(R.id.tvItem);
		ivItemImg = (ImageView) view.findViewById(R.id.ivItem);
		tvAmountMeasure = (TextView) view.findViewById(R.id.tvAmountMeasure);
		tvItemDescriptionManufacturer = (TextView) view
				.findViewById(R.id.tvItemDescriptionManufacturer);
		tvCategory = (TextView) view.findViewById(R.id.tvCategory);
		tvCategoryColor = (TextView) view.findViewById(R.id.tvCategoryColor);
		tvPriceMeasure = (TextView) view.findViewById(R.id.tvPriceMeasure);
		
		tvItem.setText(null);
		tvAmountMeasure.setText(null);
		tvItemDescriptionManufacturer.setText(null);
		tvCategory.setText(null);
		tvCategoryColor.setVisibility(View.VISIBLE);
		tvCategoryColor.setBackgroundColor(context.getResources().getColor(R.color.transperence));
		tvPriceMeasure.setText(null);
		
		// fill item_list_row.xml with data from "VwPurchaseItems"
		if (cursor.getString(categoryColor) != null) {
			tvCategoryColor.setBackgroundColor(Color.parseColor(cursor.getString(categoryColor)));
		} else {
			tvCategoryColor.setVisibility(View.INVISIBLE);
		}
		tvItem.setText(cursor.getString(item));
		String pathItemImg = cursor.getString(itemImage);
		if (pathItemImg != null) {
			ivItemImg.setImageBitmap(BitmapFactory.decodeFile(pathItemImg));
		} else {
			ivItemImg.setImageResource(R.drawable.camera);
		}
		if (cursor.getString(measureShort) != null) {
		tvAmountMeasure.setText(cursor.getString(amount).replace('.', decimalSeparator) + " "
				+ cursor.getString(measureShort));
		} else {
			tvAmountMeasure.setText(cursor.getString(amount).replace('.', decimalSeparator));
		}
		if (cursor.getString(manufacturer) != null) {
			if (cursor.getString(itemDescription) != null) {
				tvItemDescriptionManufacturer.setText(Html.fromHtml(cursor
						.getString(itemDescription)
						+ " <b>("
						+ cursor.getString(manufacturer) + ")</b>"));
			} else {
				tvItemDescriptionManufacturer.setText(Html.fromHtml(" <b>("
						+ cursor.getString(manufacturer) + ")</b>"));
			}
		} else {
			if (cursor.getString(itemDescription) != null) {
				tvItemDescriptionManufacturer.setText(cursor.getString(itemDescription));
			} else {
				tvItemDescriptionManufacturer.setText(null);
			}
		}
		if (cursor.getFloat(price) != 0 && cursor.getString(measureShort) != null) {
			tvPriceMeasure.setText(NumberFormat.getCurrencyInstance().format(Double.parseDouble(cursor.getString(price))) + " / " 	+ cursor.getString(measureShort));
		} else if (cursor.getFloat(price) != 0 && cursor.getString(measureShort) == null) {
			tvPriceMeasure.setText(NumberFormat.getCurrencyInstance().format(Double.parseDouble(cursor.getString(price))));
		} 
		if (cursor.getString(categoryName) != null ) {
			tvCategory.setText(cursor.getString(categoryName));
		}
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		return inflater.inflate(R.layout.zettl_row, null);
	}

}
